package com.yanqu.road.dao.impl.activity.langyabang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.langyabang.data.LangYaBangUserData;
import com.yanqu.road.entity.activity.langyabang.entity.LangYaUserEvent;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.commons.beanutils.PropertyUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LangYaBangUserDataDaoImpl extends TempDataObjectDao<LangYaBangUserData> {

    public static String getLangYaBangUserDataSql = "select * from `t_c_lang_ya_user_data` where `activityId`=? and `groupId`=?";
    public Map<Long, LangYaBangUserData> getLangYaBangUserData(int activityId, int groupId) {
        Map<Long, LangYaBangUserData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getLangYaBangUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                LangYaBangUserData data = parse(rs);
                dataMap.put(data.getUserId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateLangYaBangUserDataSql = "update `t_c_lang_ya_user_data` set serverId=?,buildingLevel=?,eventIdNow=?,power=?,historyPower=?,lastRecoveryTime=?,coin=?,event=?,lastBeenBeatTime=?,unionId=?,userPatronsInfoMap=?,otherGiveReward=?,otherGiveCoin=?,otherGiveScore=?,otherGiveRewardNum=?,score=?,historyCoinNum=?,drawTime=?,totalDrawTimes=?,hasAllUsedPower=?,openSwitchList=? where activityId=? and groupId=? and userId=?";
    @Override
    protected boolean updateData(LangYaBangUserData langYaBangUserData) {
        langYaBangUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangUserData.getBuildingLevel())));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getEventIdNow()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getPower()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getHistoryPower()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getLastRecoveryTime()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getCoin()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangUserData.getEvent())));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getLastBeenBeatTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, langYaBangUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangUserData.getUserPatronsInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(langYaBangUserData.getOtherGiveReward())));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getOtherGiveCoin()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getOtherGiveScore()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getOtherGiveRewardNum()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getHistoryCoinNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangUserData.getDrawTime())));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getTotalDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getHasAllUsedPower()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangUserData.getOpenSwitchList())));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getUserId()));
        if (getDb().executeNoneQuery(updateLangYaBangUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String insertLangYaBangUserDataSql = "insert into `t_c_lang_ya_user_data`(activityId,groupId,userId,serverId,buildingLevel,eventIdNow,power,historyPower,lastRecoveryTime,coin,event,lastBeenBeatTime,unionId,userPatronsInfoMap,otherGiveReward,otherGiveCoin,otherGiveScore,otherGiveRewardNum,score,historyCoinNum,drawTime,totalDrawTimes,hasAllUsedPower,openSwitchList) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(LangYaBangUserData langYaBangUserData) {
        langYaBangUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangUserData.getBuildingLevel())));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getEventIdNow()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getPower()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getHistoryPower()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getLastRecoveryTime()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getCoin()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangUserData.getEvent())));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getLastBeenBeatTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, langYaBangUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangUserData.getUserPatronsInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(langYaBangUserData.getOtherGiveReward())));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getOtherGiveCoin()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getOtherGiveScore()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getOtherGiveRewardNum()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangUserData.getHistoryCoinNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangUserData.getDrawTime())));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getTotalDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangUserData.getHasAllUsedPower()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangUserData.getOpenSwitchList())));
        if (getDb().executeNoneQuery(insertLangYaBangUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected LangYaBangUserData parseData(ResultSet rs) throws SQLException {
        LangYaBangUserData langYaBangUserData = new LangYaBangUserData();
        langYaBangUserData.setActivityId(rs.getInt("activityId"));
        langYaBangUserData.setGroupId(rs.getInt("groupId"));
        langYaBangUserData.setUserId(rs.getLong("userId"));
        langYaBangUserData.setServerId(rs.getLong("serverId"));
        langYaBangUserData.setBuildingLevel(JSON.parseObject(rs.getString("buildingLevel"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        langYaBangUserData.setEventIdNow(rs.getInt("eventIdNow"));
        langYaBangUserData.setPower(rs.getInt("power"));
        langYaBangUserData.setHistoryPower(rs.getInt("historyPower"));
        langYaBangUserData.setLastRecoveryTime(rs.getLong("lastRecoveryTime"));
        langYaBangUserData.setCoin(rs.getLong("coin"));
        langYaBangUserData.setEvent(JSON.parseObject(rs.getString("event"), LangYaUserEvent.class));
        langYaBangUserData.setLastBeenBeatTime(rs.getLong("lastBeenBeatTime"));
        langYaBangUserData.setUnionId(rs.getString("unionId"));
        langYaBangUserData.setUserPatronsInfoMap(JSON.parseObject(rs.getString("userPatronsInfoMap"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.langyabang.entity.LangYaTypePatrons>>() {}));
        langYaBangUserData.setOtherGiveReward(PropertyHelper.parseStringToProperty(rs.getString("otherGiveReward")));
        langYaBangUserData.setOtherGiveCoin(rs.getLong("otherGiveCoin"));
        langYaBangUserData.setOtherGiveScore(rs.getLong("otherGiveScore"));
        langYaBangUserData.setOtherGiveRewardNum(rs.getInt("otherGiveRewardNum"));
        langYaBangUserData.setScore(rs.getLong("score"));
        langYaBangUserData.setHistoryCoinNum(rs.getLong("historyCoinNum"));
        langYaBangUserData.setDrawTime(JSON.parseObject(rs.getString("drawTime"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        langYaBangUserData.setTotalDrawTimes(rs.getInt("totalDrawTimes"));
        langYaBangUserData.setHasAllUsedPower(rs.getInt("hasAllUsedPower"));
        String openSwitchList = rs.getString("openSwitchList");
        if (!StringUtils.isNullOrEmpty(openSwitchList)) {
            langYaBangUserData.setOpenSwitchList(JSON.parseObject(openSwitchList, new TypeReference<java.util.List<java.lang.Integer>>() {}));
        }else {
            langYaBangUserData.setOpenSwitchList(new ArrayList<>());
        }
        if (langYaBangUserData.getDrawTime() == null){
            langYaBangUserData.setDrawTime(new ConcurrentHashMap<>());
        }
        if (langYaBangUserData.getOtherGiveReward() == null){
            langYaBangUserData.setOtherGiveReward(new Property());
        }
        if (langYaBangUserData.getBuildingLevel() == null){
            langYaBangUserData.setBuildingLevel(new ConcurrentHashMap<>());
        }


        return langYaBangUserData;
    }



}
